﻿using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Web;
using System.Net;
using System.Configuration;


namespace Tickz.FileTools
{
   public class CreateHtml
    {
       
       #region   生成招商页面
       public string CreateMessage(string mcode)
       {

           string strCopy = "<!-- Created On " + DateTime.Now + "-----CopyRight by  中盛天下 -->";

           Encoding code = Encoding.GetEncoding("UTF-8");
           StringBuilder sb = new StringBuilder();
           StreamReader sr = null;
           string path1 = "";
           try
           {
               WebRequest tem = WebRequest.Create("http://" + System.Web.HttpContext.Current.Request.Url.Host + "/zhaoshang/index.htm");
               WebResponse myTem = tem.GetResponse();
               sr = new StreamReader(myTem.GetResponseStream(), code);
           }
           catch
           {
               return null;
           }

           string result;
           try
           {
               result = sr.ReadToEnd();
           }
           finally
           {
               sr.Close();
           }
           string FilePath = ConfigurationManager.AppSettings["realPath"] + @"jmzs\";
           if (!Directory.Exists(FilePath))
           {
               Directory.CreateDirectory(FilePath);

           }
          // string fileName =CommonFunction.getTopDomain(HttpContext.Current.Request.Url.Host.ToString()).Replace(".","") + "_zs.html";
           string fileName = "";
           string path = FilePath +fileName;
           result = result.Replace("$$str$$", HttpContext.Current.Server.HtmlDecode(mcode));
           if (File.Exists(path))
           {
               File.Delete(path);
           }
           result += strCopy;
           File.AppendAllText(path, result, Encoding.GetEncoding("UTF-8"));

           return path1 = "http://" + System.Web.HttpContext.Current.Request.Url.Host + "/jmzs/"+fileName; //返回页面链接
       }
       #endregion

       #region 写日志
       public static string configpath = System.AppDomain.CurrentDomain.BaseDirectory;

       public static void WriteLog(string str)
       {
           string path = configpath + "ServicesLog.txt";
           using (StreamWriter sw = File.AppendText(path))
           {
               sw.WriteLine("=========================================================");
               sw.WriteLine(DateTime.Now.ToString());
               sw.WriteLine(str);
               sw.Close();
           }
       }
       #endregion

    }
}
